package shuashuami.hb.com.avtivity;

import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.htmlutil.MyImageGetter;
import shuashuami.hb.com.htmlutil.MyTagHandler;
import shuashuami.hb.com.http.HttpCMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.http.LoginHttp;

public class YongHuXieyiActivity extends AbActivity {

    private TextView tvTitle, tvContent;

    private String id;

    @Override
    protected void initUI() {
        setAbContentView(R.layout.activity_yonghu_xieyi);
        tvTitle = (TextView) findViewById(R.id.tv_notice_title);
        tvContent = (TextView) findViewById(R.id.tv_notice_content);
    }

    @Override
    protected void initData() {
        setTitleBar("用户协议");
        setLeftView();
        getNotice();
    }

    @Override
    protected void initListener() {
        setLeftViewListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getNotice() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Message message = handler.obtainMessage(1);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);
            }
        };
        HttpCMethods.getAgreement(listener);
    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 1:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(YongHuXieyiActivity.this);
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) {
                            JSONObject temp = jsonObject.getJSONObject("content");
                            tvTitle.setText(temp.getString("title"));
                            setCaozuo(temp.getString("content"));
                        } else {
                            ToastUtil.showShort(YongHuXieyiActivity.this, jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }

            super.handleMessage(msg);
        }
    };

    private void setCaozuo(String str) {

        Spanned spanned = Html.fromHtml(str, new MyImageGetter(this, tvContent), new MyTagHandler(this));
        tvContent.setText(spanned);
        tvContent.setMovementMethod(LinkMovementMethod.getInstance());
    }


}
